package com.app.business;

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.app.dto.ItemSkuDTO;
import com.common.ConstantsSku;
import com.utils.UtilsExcel;
import com.utils.UtilsTaobao;
import com.utils.Constants;
import com.utils.UtilsString;

import jxl.Sheet;

public class ReadSkuSheet {
	private Logger log = Logger.getLogger("ReadSkuSheet");
	private String outerId = "";
	private String skuPrices = "";
	private String skuProperties = "";
	private String skuQuantities = "";
	private String propertyAlias = "";
	private String skuOuterIds = "";
	private String colorAlias = "";
	private String props = "";
	private String color = null;
	private String cid = "";
	private String outeridQuantities = "";
	private ItemSkuDTO itemSkuDTO = null;
	private List<String> queryItemPropsValueSize = null;
	private List<String> queryItemPropsValueColor = null;
	private Map<String, ItemSkuDTO> itemSkuDTOMap = new LinkedHashMap<String, ItemSkuDTO>();
	private int j = 0;
	private String[] rowData = null;
	private Map<String, String> itemsMap;
	private Map<String, Integer> columns_head_map ;

	public Map<String, ItemSkuDTO> getSkuMap(Map<String, String> itemsMap,
			Sheet sheet) {
		Map<String, ItemSkuDTO> skuMap = new HashMap<String, ItemSkuDTO>();
		List<String> list = UtilsExcel.readSheet(sheet);

		for (String data : list) {
			skuMap.putAll(convertFiled(itemsMap, data));
		}

		return skuMap;
	}

	public Map<String, ItemSkuDTO> convertFiled(Map<String, String> itemsMap,
			String data) {
		this.itemsMap = itemsMap;
		String[] splitData = data.split(Constants.SPLIT_SIGN);
		int columns_nums = UtilsExcel.judgeColumnNum(splitData);
		int data_nums = columns_nums + 1;
		String[] skuSheet_columns_head = splitData[columns_nums].split(Constants.SPLIT_SIGN_CELL);
		columns_head_map = new HashMap<String, Integer>();
		for (int i = 0; i < skuSheet_columns_head.length; i++) {
			columns_head_map.put(skuSheet_columns_head[i], i);
		}

		for (int i = data_nums; i < splitData.length; i++) {
			rowData = splitData[i].split(Constants.SPLIT_SIGN_CELL);

			if (i == 1 || rowData[columns_head_map.get(ConstantsSku.SKU_ITEM_CODE)].length() > 1) {				
				setItemSkuDTO(false);
			}
			
			boolean flag = true;
			if (itemsMap != null) {
				if (!StringUtils.isBlank(rowData[columns_head_map.get(ConstantsSku.SKU_COLOR_CODE)])) {//说明读取到一个新的color
					/*	
					 * 
					 * 废弃color从此excel中获取
					 *	color = rowData[columns_head_map.get(ConstantsSku.SKU_COLOR_CODE)];
					colorAlias = colorAlias + queryItemPropsValueColor.get(j)
							+ ":" + color + ";";
					color = queryItemPropsValueColor.get(j);*/
//					if(color == null){
//					}
					flag = true;
				} else {
					flag = false;
				}
			}
			String skuStr = rowData[columns_head_map.get(ConstantsSku.SKU_STR)];
			if(!StringUtils.isBlank(skuStr)){
				String[] skuStrSplit = skuStr.split("_");
				if (itemsMap != null) {
					skuOuterIds = skuOuterIds + skuStr + ",";
					if (skuStrSplit.length <= 2 || skuStrSplit[2].equals("/")) {
						skuProperties = skuProperties + color + ";";
					} else {
						String chima = skuStrSplit[2];
						propertyAlias = propertyAlias
						+ queryItemPropsValueSize.get(j) + ":" + chima
						+ ";";
						skuProperties = skuProperties + color + ";"
						+ queryItemPropsValueSize.get(j) + ",";
						if (flag) {
							props = props + color + ";"
							+ queryItemPropsValueSize.get(j) + ";";
						} else {
							props = props + queryItemPropsValueSize.get(j) + ";";
						}
					}
					
					skuQuantities = skuQuantities + rowData[columns_head_map.get(ConstantsSku.SKU_NUMS)] + ",";
					skuPrices = skuPrices + rowData[columns_head_map.get(ConstantsSku.SKU_PRICE)] + ",";
				}
				outeridQuantities = outeridQuantities + skuStr + ":"
				+ rowData[columns_head_map.get(ConstantsSku.SKU_NUMS)] + ";";
				j = j + 1;
				
			}
			if (i == splitData.length - 1) {
				setItemSkuDTO(true);
			}
		}

		return itemSkuDTOMap;
	}

	public void setItemSkuDTO(boolean isOver){
		if(itemSkuDTO != null){
			itemSkuDTO.setSkuProperties(skuProperties);
			itemSkuDTO.setSkuQuantities(skuQuantities);
			itemSkuDTO.setSkuPrices(skuPrices);
			itemSkuDTO.setPropertyAlias(colorAlias + propertyAlias);
			itemSkuDTO.setProps(props);
			itemSkuDTO.setSkuOuterIds(skuOuterIds);
			itemSkuDTO.setOuteridQuantities(outeridQuantities);
			itemSkuDTOMap.put(itemSkuDTO.getOuterId(), itemSkuDTO);
		}
		
		if(!isOver){
			skuPrices = "";
			skuProperties = "";
			propertyAlias = "";
			skuQuantities = "";
			colorAlias = "";
			props = "";
			skuOuterIds = "";
			outeridQuantities = "";
			j = 0;
			outerId = rowData[columns_head_map.get(ConstantsSku.SKU_ITEM_CODE)];
			if (itemsMap != null && itemsMap.containsKey(outerId)) {
				cid = UtilsString.parseTagBody(itemsMap.get(outerId), "cid");
				queryItemPropsValueSize = UtilsTaobao.queryItemPropsPidVid(cid,
				"尺码");
				
				queryItemPropsValueColor = UtilsTaobao.queryItemPropsPidVid(cid,
					"颜色");
				
				color = UtilsString.parseTagBody(itemsMap.get(outerId), "color");
				try {
					String color1 = null;
					if(color.contains("色")){
						color1 = UtilsTaobao.queryItemProps(cid, "颜色", color);
					}else{
						color1 = UtilsTaobao.queryItemProps(cid, "颜色", color + "色");
					}
					if(color1 == null){
						color1 = queryItemPropsValueColor.get(0);
						colorAlias = colorAlias + color1 + ":" + color + ";";
					}
					color = color1.replace(";", "");
				} catch (IOException e) {
					e.printStackTrace();
				}
			}else{
				log.warn(outerId + ",sku表有，但属性表没有!此商品未添加!");
			}
			itemSkuDTO = new ItemSkuDTO();
			itemSkuDTO.setOuterId(outerId);
		}
		
	}

}
